package com.freesky.biz.icbc.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.freesky.biz.icbc.domain.TAigouFriday;
import com.freesky.biz.icbc.domain.TWomenDay;
import com.freesky.biz.icbc.domain.TWomenProduct;
import com.freesky.biz.icbc.mapper.AigouFridayMapper;
import com.freesky.biz.icbc.mapper.WomenDayMapper;
import com.freesky.biz.icbc.service.IAigouFridayService;
import com.freesky.biz.icbc.service.IWomenDayService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.core.redis.RedisCache;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WomenDayImpl implements IWomenDayService {

    @Autowired
    private WomenDayMapper womenDayMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<TWomenDay> findList(TWomenDay womenDay) {
        return womenDayMapper.findList(womenDay);
    }

    @Override
    public AjaxResult exportFile(TWomenDay womenDay) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("WOMEN_DAY");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(womenDay, new TWomenDay(), IWomenDayService.class,
                "findList", "妇女节",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TWomenProduct> selectWowenProductList(String startDt) {
        return womenDayMapper.selectWowenProductList(startDt);
    }

    @Override
    public void womenDayInit() {
        String startDt = DateUtil.format(new Date(),"yyyy-MM-dd");
        List<TWomenProduct> womenProductList = womenDayMapper.selectWowenProductList(startDt);
        for(int i=0;i<womenProductList.size();i++){
            TWomenProduct womenProduct = womenProductList.get(i);
            Integer leftCount = womenProduct.getTotalCount() - womenProduct.getUseCount();
            womenProduct.setLeftCount(leftCount);
            womenProductList.set(i,womenProduct);
            setWowenProductToRedis("WOMEN:PD:"+womenProduct.getType(),womenProduct);
        }
        //总产品列表
        redisCache.setCacheObject("WOMEN:PDLIST",JSON.toJSONString(womenProductList));
    }

    private void setWowenProductToRedis(String key, TWomenProduct womenProduct) {
        System.out.println("setWowenProductToRedis:"+key+","+ com.alibaba.fastjson.JSON.toJSONString(womenProduct));
        redisCache.hset(key, "id", womenProduct.getId());
        redisCache.hset(key, "name", womenProduct.getName());
        redisCache.hset(key, "areaCode", womenProduct.getAreaCode());
        redisCache.hset(key, "cardFlag", womenProduct.getCardFlag());
        redisCache.hset(key, "type", womenProduct.getType());
        redisCache.hset(key, "amt", womenProduct.getAmt());
        redisCache.hset(key, "stockId", womenProduct.getStockId());
        redisCache.hset(key, "totalCount", womenProduct.getTotalCount());
        redisCache.hset(key, "useCount", womenProduct.getUseCount());
        redisCache.hset(key, "leftCount", womenProduct.getLeftCount());
    }

}
